<template :page.sync="page">
  <div class="el-card is-always-shadow el-card__body">

<avue-crud
  ref="crud"
  :option="option"
  :table-loading="listLoading"
  :data="reportApplyTableData"
  :page.sync="page"
  :search.sync="searchForm"
  :row-style="rowStyle"
  @search-change="handleFilter"
  @selection-change="selectionChange"
  @refresh-change="search"
  @on-load="search">
  <template slot="searchMenu" slot-scope="{row,size}">
    <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm);search()}" icon="el-icon-delete">清空</el-button>
  </template>
  <template slot-scope="scope" slot="lineCodeSearch">
    <el-select v-model="searchForm.lineCode" placeholder="线路" @change="changeLine" clearable filterable>
      <el-option
        v-for="(item,index) in lineCodes"
        :key="index"
        :label="item.name"
        :value="item.code">
      </el-option>
    </el-select>
  </template>
  <template slot="stationCodeSearch" slot-scope="scope">
    <el-select v-model="searchForm.stationCode" placeholder="车站" clearable filterable>
      <el-option
        v-for="(item,index) in stationCodes"
        :key="index"
        :label="item.name"
        :value="item.code">
      </el-option>
    </el-select>
  </template>
  <template slot="search" slot-scope="{row,size}" >
    <!-- <el-form-item label="差异金额范围" labelWidth="150px" style="width:450px">
      <el-input-number v-model="searchForm.errorAmountMin" :min="0" :max="99999998" label="差异金额范围" clearable></el-input-number>~
      <el-input-number v-model="searchForm.errorAmountMax" :min="1" :max="99999999" label="差异金额范围" clearable></el-input-number>
    </el-form-item> -->
    <el-form-item label="报表名称">
      <el-select v-model="searchForm.reportNameId" placeholder="报表名称" clearable>
        <el-option
          v-for="(item,index) in reportNameIds"
          :key="index"
          :label="item.reportName"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="申请时间">
      <el-date-picker
        clearable
        v-model="searchForm.applyTime"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="报表时间">
      <el-date-picker
        clearable
        v-model="searchForm.reprotTime"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="修改状态">
          <el-select
            :size="size"
            v-model="searchForm.isModify"
            type="select"
            placeholder="修改状态">
            <el-option value="1" label="已完成"></el-option>
            <el-option value="2" label="修改中"></el-option>
            <el-option value="0" label="未完成"></el-option>
          </el-select>
        </el-form-item>
  </template>


<template slot="menuRight">
    <el-button type="primary" size="mini" @click="editor(null,3,'添加')">添加</el-button>
  <el-button type="primary" size="mini" @click="deleteReportGGSQ()">删除</el-button>
    <el-button type="primary" size="mini" @click="submitData()">提交</el-button>
  </template>
  <template slot-scope="scope" slot="menu">
    <el-button type="text" size="small" plain @click.stop="previewEditor(scope.row,1,'预览')">预览</el-button>
    <el-button type="text" size="small" v-if="scope.row.checkStatus==1 || scope.row.checkStatus==4 " plain @click.stop="editor(scope.row,2,'编辑')">编辑</el-button>
<!--    <el-button type="text" size="small" v-if="scope.row.checkStatus==3 || scope.row.isModify==0 && (scope.row.checkStatus!=1 && scope.row.checkStatus!=4 && scope.row.checkStatus!=2)" plain @click.stop="editor(scope.row,4,'编辑')">编辑</el-button>-->
<!--    <el-button type="text" size="small" plain v-if="!scope.row.checkStatus || scope.row.checkStatus<2" @click.stop="deleteReportGGSQ(scope.row)">删除</el-button>-->
  </template>
</avue-crud>
<Editor ref="Editor"></Editor>
<PREVIEW ref="PREVIEW" ></PREVIEW>
</div>

</template>

<script>
  import Editor from './Editor.vue';
  import PREVIEW from './pre.vue';
  import {getStationByCode,getLineAndStationByCode,setSearchFiledsEmpty,getSwPmReportPath} from "../../../../../api/subwayRat/commonCheck";
  import {
    deleteReportGGSQ,
    get_xw_ReportErrorList, submit_xw_ReportPromise,
    XW_SQ_tableOption
  } from "../../../../../api/stationTiecketManager/xianWngCJXXQR/BBCCGG/BBCCGGSQ/KZDLCK";
  import {exportDataToExcel} from "../../../../../api/subwayRat/exportSelf";
  import {getLineLists} from "../../../../../api/subwayRat/reportMondify/reportMistakeInfo";
  export default {
    name: 'reportApplyInfo',
    components:{Editor,PREVIEW
    },
    data() {
      return {
        listLoading: true,
        searchForm:{},
        lineCodes:[],
        stationCodes:[],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条,
          isAsc: false// 是否倒序
        },
        isShowReportMistakeInfoEditor:false,//是否加载编辑页面
        reportApplyTableData: [],
        reportApplyTableCheck:[],
        selectKeyVals:[],
        option:{},
        addModifyLimitTimeVisible : false,
        reportNameIds:[],
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        }
      }
    },
    created() {
      this.option=XW_SQ_tableOption;
    },
    mounted() {
      this.search();
      this.initLine();
      this.initReportNameIds();
    },
    methods: {
      setSearchFiledsEmpty,
      initReportNameIds(){ //初始化报表名
        getSwPmReportPath().then(res=>{
          this.reportNameIds=res.data.data;
        })
      },
      previewEditor(row,showType,title){
        this.$nextTick(()=>{
          this.$refs.PREVIEW.previewEditor(row,showType,title);
        });

      },
      rowStyle({ row, column, rowIndex }) {
        if (row.checkStatus == '1') {
          return {
            backgroundColor: "#eee",
            color: "red",
          };
        }
      },
      editor(row,showType,title){
        this.$nextTick(()=>{
          this.$refs.Editor.previewEditor(row,showType,title);
        });
      },
      changeLine(v){
        let p={};
        p.userCode=this.$store.getters.userInfo.userCode;
        p.lineCode=v;
        getStationLists(p).then((res)=>{
          this.stationCodes=res.data.data;
        })
      },
      async submitData(){
        if(this.reportApplyTableCheck.length == 0){
          this.$message({message: '请选择数据',type: 'warning'});
          return;
        }
        for(let i=0;i<this.reportApplyTableCheck.length;i++) {
          if (this.reportApplyTableCheck[i].checkStatus != 1) {
            this.$message({message: '请选择已申请状态数据', type: 'warning'});
            return;
          }
        }

        let messageLSList = [];
        for(let i=0;i<this.reportApplyTableData.length;i++){
          let data = await getLineAndStationByCode({"lineCode":this.reportApplyTableData[i].lineCode, "code":this.reportApplyTableData[i].stationCode});
          messageLSList.push({
            "content":"车站：" + data.data[0].name,
            "lineCode":this.reportApplyTableData[i].lineCode,
            "stationCode":this.reportApplyTableData[i].stationCode
          })
        }
        var ids=[];
        this.reportApplyTableCheck.forEach(function (key, val) {
          ids[val] = key.id
        })
        let p = {
          messageLSList:JSON.stringify(messageLSList),
          //title:this.$store.getters.tag.label,
	  title:'报表差错更改申请',
          msgType:"审批",
          type:"reportSubmit_001",
          ids : JSON.stringify(ids),
          idlist: ids.join(',')
        };
          submit_xw_ReportPromise(p).then(res=>{
            if(res.data.code==0){
                this.$message.success("操作成功");
                this.search();
            }else{
              this.$message.error("操作失败");
            }
          });
      },
      deleteReportGGSQ(){ //删除数据
        if(this.reportApplyTableCheck.length == 0){
          this.$message({message: '请选择数据',type: 'warning'});
          return;
        }
        for(let i=0;i<this.reportApplyTableCheck.length;i++){
          if(this.reportApplyTableCheck[i].checkStatus==2 || this.reportApplyTableCheck[i].checkStatus==3){
            this.$message({message: '请选择未提交或者审批不通过数据',type: 'warning'});
            return;
          }
        }
        deleteReportGGSQ({ids:this.selectKeyVals.join(',')}).then(res=>{
          if(res.data.code==0){
            this.$message.success("操作成功");
            this.search();
          }else{
            this.$message.error("操作失败");
          }
        })
      },
      search(){
        // debugger
        Object.assign(this.searchForm,this.page);
        this.searchForm.pageNo = null;
        this.searchForm.userCode=this.$store.getters.userInfo.userCode;
        this.searchForm.lineCode = this.$store.getters.userInfo.lineCode;
        this.searchForm.stationCode = this.$store.getters.userInfo.stationCode;
        this.searchForm.dataSource = 1;
        get_xw_ReportErrorList(this.searchForm).then((res)=>{
          this.reportApplyTableData=res.data.data;
          this.page = res.data;
          this.page.data = null;
          this.listLoading=false;
        });
      },
      clear(){
        this.searchForm = {};
        // this.searchForm.errorAmountMin = 0;
        // this.searchForm.errorAmountMax = 1;
      },
      selectionChange(list){
        this.reportApplyTableCheck=list;
        this.selectKeyVals=[];
        for(let i=0;i<list.length;i++){
          this.selectKeyVals.push(list[i].id);
        }
      },
      handleFilter(params, done) {
        Object.assign(this.searchForm,params);
        this.search()
        done()
      },
      initLine(){
        let p={};
        p.userCode=this.$store.getters.userInfo.userCode;
        getLineLists(p).then((res)=>{
          this.lineCodes=res.data.data;
        });
      }
    }
  }
</script>
<style lang="scss">

</style>
